#include<iostream>
#include<cstring>
using namespace std;
const int N = 2e4 + 10, M = N * 2;
int h[N], e[M], ne[M], idx;
int f[N];
int w[N];
int n, m;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u, int fa)
{
	f[u] = w[u];
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == fa)continue;
		dfs(j, u);
		if (f[j] < 0)continue;
		f[u] += f[j];
	}
}
int main()
{
	cin >> n;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++)cin >> w[i];
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		add(b, a);
	}
	dfs(1, 0);
	int ma = -0x3f3f3f3f;
	for (int i = 1; i <= n; i++) {
		//cout << "i==" << i << "  f[i]==" << f[i] << endl;
		ma = max(ma, f[i]);
	}
	cout << ma << endl;
	return 0;
}